/**
 * ...
 * @author waneck
 */

package asc;

extern class Application 
{
	
	/**
	 * A boolean value that lets administrators access an application with the Administration API
	 * approveDebugSession()  method (true) or not (false). A debug connection lets administrators
	 * view information about shared objects and streams in the Administration Console. 
	 */
	var allowDebug:Bool;
	
	/**
	 * Provides access to properties of the ApplicationObject  element in the Application.xml
	 * configuration file. To access properties that you set in the configuration file, use the
	 * application.config  property. For example, to set the value of the password  element, use the
	 * code application.config.password.
	 */
	var config:Dynamic<String>;
	
	/**
	 * Read-only; the host name of the server for default virtual hosts; the virtual host name for all
	 * other virtual hosts.
	 * 
	 * If an application is running on the default virtual host, and if a value is set in the ServerDomain
	 * element in the Server.xml configuration file, the application.hostname property contains the value
	 * set in the ServerDomain element. If a value has not been set in the ServerDomain element, the property
	 * is undefined.
	 * 
	 * If an application is running on any virtual host other than the default, the application.hostname
	 * property contains the name of the virtual host.
	 */
	var hostname(default, null):String;
	
	/**
	 * Read-only; the name of the application instance.
	 */
	var name(default, null):String;
	
	/**
	 * Read-only; the platform and version of the server.
	 */
	var server(default, null):String;
	
	/**
	 * Accepts a connection call from a client to the server. 
	 * 
	 * @details
	 * When NetConnection.connect() is called from the client side, it passes a Client object to 
	 * application.onConnect() on the server. Call application.acceptConnection() in an
	 * application.onConnect() event handler to accept a connection from a client. When
	 * this method is called, NetConnection.onStatus() is invoked on the client with the
	 * info.code property set to "NetConnection.Connect.Success".
	 * 
	 * You can use the application.acceptConnection() method outside an application.onConnect()
	 * event handler to accept a client connection that had been placed in a pending state (for
	 * example, to verify a user name and password).
	 * 
	 * When you call this method, NetConnection.onStatus() is invoked on the client with the
	 * info.code property set to "NetConnection.Connect.Success". For more information, see
	 * the NetStatusEvent.info property in the ActionScript 3.0 Language and Components Reference
	 * or the NetConnection.onStatus() entry in the Adobe Flash Media Server ActionScript 2.0 Language Reference.
	 * 
	 * @example
	 * application.onConnect = function(client){ 
	 * 		// Accept the connection. 
	 * 		application.acceptConnection(client); 
	 * 		trace("connect: " + client.id); 
	 * };
	 * 
	 */
	function acceptConnection():Void;
	
	/**
	 * Broadcasts a message to all clients connected to an application instance.
	 * To handle the message, the client must define a handler on the NetConnection
	 * object with the same name as the cmd parameter.
	 * 
	 * @param	cmd			A string; a message to broadcast. To handle the message,
	 * 						  define a handler with the same name as cmd on the client-side NetConnection object.
	 * @param	?cmd2..6	A string; additional messages to broadcast. 
	 */
	function broadcastMsg(cmd:String, ?cmd2:String, ?cmd3:String, ?cmd4:String, ?cmd5:String, ?cmd6:String):Void;
	
	/**
	 * Deletes persistent shared objects files (FSO files) specified by the soPath
	 * parameter and clears all properties from active shared objects (persistent
	 * and nonpersistent). Even if you have deleted all the properties from a persistent
	 * shared object, unless you call clearSharedObjects(), the FSO file still exists on the server.
	 * 
	 * @param	soPath	A string indicating the Uniform Resource Identifier (URI) of a shared object.
	 * 
	 * @usage
	 * 		/ clears all local and persistent shared objects associated with the instance.
	 * 		/foo/bar clears the shared object /foo/bar; if bar is a directory name, no shared objects are deleted.
	 * 		/foo/bar/* clears all shared objects stored under the instance directory /foo/bar.
	 * 		 If no persistent shared objects are in use within this namespace, the bar directory is also deleted. 
	 * 
	 * @return A boolean value of true if the shared object at the specified path was deleted; 
	 * otherwise, false. If wildcard characters are used to delete multiple files, the method returns
	 * true only if all the shared objects that match the wildcard pattern were successfully deleted;
	 * otherwise, it returns false.
	 */
	function clearSharedObjects(soPath:String):Bool;
	
	/**
	 * Clears recorded streams files associated with an application instance. You can use
	 * this method to clear a single stream, all streams associated with the application
	 * instance, just those streams in a specific subdirectory of the application instance,
	 * or just those streams whose names match a specified wildcard pattern.
	 * 
	 * If the clearStreams() method is invoked on a stream that is currently recording, 
	 * the recorded file is set to length 0 (cleared), and the internal cached data is also cleared.
	 * 
	 * A call to application.clearStreams() invokes the Stream.onStatus() handler and
	 * passes it an information object that contains information about the success
	 * or failure of the call.
	 * 
	 * @param	streamPath	A string indicating the Uniform Resource Identifier (URI) of a stream.
	 * 
	 * The streamPath parameter specifies the location and name of a stream relative to the directory 
	 * of the application instance. You can include a slash (/) as a delimiter between directories in 
	 * the path. The last element in the path can contain wildcard patterns (for example, a question mark 
	 * [?] and an asterisk [*]) or a stream name. The clearStreams() method traverses the stream hierarchy
	 * along the specified path and clears all the recorded streams that match the given wildcard pattern. 
	 * Specifying a slash clears all the streams that are associated with an application instance.
	 * 
	 * To clear FLV, F4V, or MP3 files, precede the stream path with flv:, mp4:, or mp3:. When you specify 
	 * flv: or mp3: you don’t have to specify a file extension; .flv and .mp3 are implied. However, when 
	 * you call application.clearStreams("mp4:foo"), the server deletes any file with the name “foo” in an 
	 * MPEG-4 container; for example, foo.mp4, foo.mov, and foo.f4v. To delete a specific file, pass the file 
	 * extension in the call; for example, application.clearStreams("mp4:foo.f4v").
	 * 
	 * @usage
	 * 		flv:/ clears all FLV streams associated with the application instance.
	 * 		mp3:/ clears all MP3 files associated with the application instance.
	 * 		mp4:foo.mp4 clears the foo.mp4 file.
	 * 
	 * @return
	 * A boolean value of true if the stream at the specified path was deleted; otherwise, false.
	 * If wildcard characters are used to clear multiple stream files, the method returns true only
	 * if all the streams that match the wildcard pattern were successfully deleted; otherwise, it returns false.
	 */
	function clearStreams(streamPath:String):Void;
	
	/**
	 * Terminates a client connection to the application. When this method is called, NetConnection.onStatus()
	 * is invoked on the client with info.code set to "NetConnection.Connect.Closed". The application.onDisconnect()
	 * handler is also invoked.
	 * 
	 * @param	clientObj	    A Client object indicating the client to disconnect. The object must
	 * 								be a Client object from the application.clients  array.
	 * 
	 * @return	A boolean value of true if the disconnection was successful; otherwise, false.
	 */
	function disconnect(clientObj:Client):Bool;
	
	/**
	 * Invokes the garbage collector to reclaim any unused resources for this application instance.
	 */
	function gc():Void;
	
	/**
	 * Returns statistics about an application.
	 * 
	 * @return	An Object whose properties contain statistics about the application instance
	 * @see 	asc.Application.StatsProps
	 */
	function getStats():StatsProps;
	
	//////////////////     EVENTS
	
	/**
	 * Invoked when the server first loads the application instance. Use this handler to initialize
	 * an application state. The onAppStart() event is invoked only once during the lifetime of an
	 * application instance. 
	 */
	dynamic function onAppStart():Void;
	
	/**
	 * Invoked when the server is about to unload an application instance. You can use onAppStop()
	 * to flush the application state or to prevent the application from being unloaded.
	 * 
	 * Define a function that is executed when the event handler is invoked. If the function returns
	 * true, the application is unloaded. If the function returns false, the application is not unloaded.
	 * If you don’t define a function for this event handler, or if the return value is not a boolean value,
	 * the application is unloaded when the event is invoked.
	 * 
	 * The Flash Media Server application passes an information object to the application.onAppStop() event.
	 * You can use Server-Side ActionScript to look at this information object to decide what to do in the
	 * function you define. You can also use the application.onAppStop() event to notify users before shutdown.
	 * 
	 * If you use the Administration Console or the Server Administration API to unload a Flash Media Server
	 * application, application.onAppStop() is not invoked. Therefore you cannot use application.onAppStop()
	 * to tell users that the application is exiting.
	 * 
	 * @return	To unload the application, return true or any non-false value.
	 * 			To refuse to unload the application, return false. 
	 */
	dynamic function onAppStop(info:{code:String, level:String}):Bool;
	
	/**
	 * Invoked when NetConnection.connect() is called from the client. This handler is passed a Client
	 * object representing the connecting client. Use the Client object to perform actions on the client
	 * in the handler. For example, use this function to accept, reject, or redirect a client connection,
	 * perform authentication, define methods on the Client object to be called remotely from NetConnection.call(),
	 * and set the Client.readAccess  and Client.writeAccess  properties to determine client access rights
	 * to server-side objects.
	 * 
	 * When performing authentication, all of the information required for authentication should be sent from
	 * the NetConnection.connect() method to the onConnect() handler as parameters (p1..., pN).
	 * 
	 * If you don’t define an onConnect() handler, connections are accepted by default.
	 * 
	 * If there are several simultaneous connection requests for an application, the server serializes the
	 * requests so that only one application.onConnect() handler is executed at a time. It’s a good idea to
	 * write code for the application.onConnect() function that is executed quickly to prevent a long connection
	 * time for clients.
	 * 
	 * @param	clientObj	A Client object. This object contains information about the client that is
	 * 						  connecting to the application.
	 * @param	?p1..p7		Optional parameters passed to the application.onConnect()  handler from the
	 * 						  client-side NetConnection.connect()  method when a client connects to the application.
	 * @return	A boolean value; true causes the server to accept the connection; false causes the server to reject the connection. 
	 * 
	 * When true is returned, NetConnection.onStatus() is invoked on the client with info.code set to
	 * "NetConnection.Connect.Success". When false is returned, NetConnection.onStatus() is invoked on the
	 * client with info.code set to "NetConnection.Connect.Rejected".
	 * 
	 * If null or no value is returned, the server puts the client in a pending state and the client can’t receive
	 * or send messages. If the client is put in a pending state, you must call application.acceptConnection()
	 * or application.rejectConnection() at a later time to accept or reject the connection.
	 * For example, you can perform external authentication by making a NetConnection call in your
	 * application.onConnect() event handler to an application server and having the reply handler call
	 * application.acceptConnection() or application.rejectConnection(), depending on the information received by
	 * the reply handler.
	 * 
	 * You can also call application.acceptConnection() or application.rejectConnection() in the
	 * application.onConnect() event handler. If you do, any value returned by the function is ignored.
	 */
	dynamic function onConnect(clientObj:Client, ?p1:Dynamic, ?p2:Dynamic, ?p3:Dynamic, ?p4:Dynamic, ?p5:Dynamic, ?p6:Dynamic, ?p7:Dynamic):Bool;
	
	/**
	 * Invoked when a client successfully connects to an application; for use with version 2 components only.
	 * Use onConnectAccept() to handle the result of an accepted connection in an application that contains components.
	 * 
	 * If you don’t use the version 2 components framework (ActionScript 2.0 components), you can execute code in the
	 * application.onConnect() handler after accepting or rejecting the connection. When you use the components framework,
	 * however, any code that you want to execute after the connection is accepted or rejected must be placed in the
	 * application.onConnectAccept() and application.onConnectReject() event handlers. This architecture allows all of
	 * the components to decide whether a connection is accepted or rejected. 
	 * 
	 * @param	clientObj	A Client object; the client connecting to the application.
	 * @param	?p1...p7	Optional parameters passed to the application.onConnectAccept() method.
	 * 						These parameters are passed from the client-side NetConnection.connect()
	 * 						method when a client connects to the application;
	 */
	dynamic function onConnectAccept(clientObj:Client, ?p1:Dynamic, ?p2:Dynamic, ?p3:Dynamic, ?p4:Dynamic, ?p5:Dynamic, ?p6:Dynamic, ?p7:Dynamic):Void;
	
	/**
	 * Invoked when a connection is rejected in an application that contains components.
	 * 
	 * If you don’t use the version 2 components framework, you can execute code in the
	 * application.onConnect() handler after accepting or rejecting a connection. When you use the
	 * components framework, however, any code that you want to execute after the connection is accepted 
	 * or rejected must be placed in the application.onConnectAccept() and application.onConnectReject()
	 * framework event handlers. This architecture allows all of the components to decide whether a connection
	 * is accepted or rejected.
	 * 
	 * @param	clientObj	A Client object; the client connecting to the application.
	 * @param	?p1...p7	Optional parameters passed to the application.onConnectAccept() method.
	 * 						These parameters are passed from the client-side NetConnection.connect()
	 * 						method when a client connects to the application;
	 */
	dynamic function onConnectReject(clientObj:Client, ?p1:Dynamic, ?p2:Dynamic, ?p3:Dynamic, ?p4:Dynamic, ?p5:Dynamic, ?p6:Dynamic, ?p7:Dynamic):Void;
	
	/**
	 * Invoked when a client disconnects from an application. Use this event handler to flush any client
	 * state information or to notify other users that a user is leaving the application. This handler is optional. 
	 * 
	 * @param	clientObj	A Client object; a client disconnecting from the application.
	 */
	dynamic function onDisconnect(clientObj:Client):Void;
	
	/**
	 * Invoked when a client publishes a stream to an application. Use this event handler to send
	 * traffic to other servers when you’re building a large-scale live broadcasting application;
	 * this is called multipoint publishing. For example, you can support subscribers in multiple
	 * geographic locations by sending traffic from the origin server (Server A) in one city to
	 * two origin servers in two different cities (Server B and Server C). The following is the
	 * workflow for such a scenario:
	 * 
	 *    1. A client publisher connects to Server A and starts publishing.
	 *    2. Server A receives notifications from the event handler application.onPublish() in a server-side script.
	 *    3. Inside the onPublish() handler, create two NetStream objects to Server B and Server C.
	 *    4. Call the NetStream.publish() method to redirect the publishing data from Server A to
	 * 		 Server B and Server C.
	 *    5. Subscribers connecting to Server B and Server C get the same live stream.
	 * 
	 * @param	clientObj	A Client object; the client publishing the stream to the application.
	 * @param	streamObj	A Stream object; the stream being published to the application.
	 */
	dynamic function onPublish(clientObj:Client, streamObj:Stream):Void;
	
	/**
	 * Invoked when the server encounters an error while processing a message that was targeted at this 
	 * application instance. The application.onStatus() handler handles any Stream.onStatus() or
	 * NetConnection.onStatus() messages that don’t find handlers. Also, there are a few status calls
	 * that come only to application.onStatus().
	 * 
	 * @param	infoObject	An Object with code and level properties that contain information about the
	 * 	status of an application. Some information objects also have details and description properties.
	 */
	dynamic function onStatus(infoObject: { code:String, level:String, details:Dynamic<String> } ):Void;
	
	/**
	 * Invoked when a client stops publishing a stream to an application.
	 * 
	 * Use this event handler with application.onPublish()to send traffic to other
	 * servers when you’re building a large-scale, live broadcasting application. 
	 * 
	 * @param	clientObject	A Client object; the client publishing the stream to the application.
	 * @param	streamObj		A Stream object; the stream being published to the application.
	 */
	dynamic function onUnpublish(clientObject:Client, streamObj:Stream):Void;
	
	///////////////////IMPERATIVE COMMANDS
	
	/**
	 * Rejects a connection and provides a redirect URL. You must write logic in the NetConnection.onStatus()
	 * handler that detects redirection and passes the new connection URL to the NetConnection.connect() method.
	 * 
	 * 
	 * @param	clientObj		A Client object specifying a client to reject. 
	 * @param	?url			A string specifying the new connection URL. If you omit rejectConnection() is called instead. 
	 * @param	?description	A string that lets you provide more information when a connection is redirected. 	
	 * @param	?url			An object of any type that is sent to the client, explaining the reason for rejection.
	 * 							The errorObj object is available in client-side scripts as the application property of
	 * 							the information object that is passed to the NetConnection.onStatus()
	 * 							call when the connection is rejected.
	 */
	function redirectConnection(clientObj:Client, ?url:String, ?description:String, ?errorObj:Dynamic):Void;
	
	/**
	 * Registers a constructor function that is used when deserializing an object of a certain class type. If the
	 * constructor for a class is not registered, you cannot call the deserialized object’s methods. This method
	 * is also used to unregister the constructor for a class. This is an advanced use of the server and is
	 * necessary only when sending ActionScript objects between a client and a server.
	 * 
	 * The client and the server communicate over a network connection. Therefore, if you use typed objects, each
	 * side must have the prototype of the same objects they both use. In other words, both the client-side and
	 * Server-Side ActionScript must define and declare the types of data they share so that there is a clear,
	 * reciprocal relationship between an object, method, or property on the client and the corresponding element
	 * on the server. You can call application.registerClass() to register the object’s class type on the server
	 * side so that you can use the methods defined in the class. 
	 * 
	 * @param	className	A string indicating the name of an ActionScript class.
	 * @param	constructor	A constructor function used to create an object of a
	 * 						specific class type during object deserialization.
	 * 						The name of the constructor function must be the same
	 * 						as className. During object serialization, the name of
	 * 						the constructor function is serialized as the object’s type.
	 * 						To unregister the class, pass the value null as the constructor
	 * 						parameter.
	 */
	function registerClass(className:String, constructor:Void->Void):Void;
	
	/**
	 * Maps a method call to another function. You can use this method to communicate between different
	 * application instances that can be on the same Flash Media Server or on different Flash Media
	 * Servers. Clients can execute server-side methods of any application instances to which they are 
	 * connected. Server-side scripts can use this method to register methods to be proxied to other
	 * application instances on the same server or a different server. You can remove or unregister the
	 * proxy by calling this method and passing null  for the proxyConnection parameter, which results
	 * in the same behavior as never registering the method at all.
	 * 
	 * @param	methodName			A string indicating the name of a method. All requests to execute
	 * 								methodName for this application instance are forwarded to the 
	 * 								proxyConnection object.
	 * @param	proxyConnection		A Client or NetConnection object. All requests to execute the remote
	 * 								method specified by methodName are sent to the Client or NetConnection 
	 * 								object specified in the proxyConnection parameter. Any result returned
	 * 								is sent back to the originator of the call.
	 * 								To unregister or remove the proxy, provide a value of null for this parameter.
	 * @param	proxyMethodName		A string indicating the name of a method for the server to call on the
	 * 								object specified by the proxyConnection  parameter if proxyMethodName
	 * 								is different from the method specified by the methodName parameter.
	 * 								This is an optional parameter.
	 */
	function registerProxy(methodName:String, proxyConnection:Dynamic, proxyMethodName:String):Void;
	
	/**
	 * Rejects the connection call from a client to the server. The application.onConnect() handler is
	 * invoked when the client calls NetConnection.connect(). In the application.onConnect() handler, 
	 * you can either accept or reject the connection. You can also make a call to an application server
	 * to authenticate the client before you accept or reject it.
	 * 
	 * @param	clientObj		A Client object specifying a client to reject. 
	 * @param	?description	A string that allows you to provide more information when a connection is redirected. 
	 * @param	?errObj			An object of any type that is sent to the client, explaining the reason for rejection.
	 * 							The errObj object is available in client-side scripts as the application  property of
	 * 							the information object that is passed to the NetConnection.onStatus()  call when the
	 * 							connection is rejected.
	 */
	function rejectConnection(clientObj:Client, ?description:String, ?errObj:Dynamic):Void;
	
	/**
	 * Unloads the application instance. If the application is running in vhost or application-level scope,
	 * only the application instance is unloaded, but the core process remains running. If the application
	 * is running in instance scope, the application instance is unloaded and the core process terminates. 
	 * This process is done asynchronously; the instance is unloaded when the unload sequence begins, not 
	 * when the shutdown() call returns.
	 * 
	 * After shutdown() is called, application.onAppStop() is called, connected clients are disconnected,
	 * and application.onDisconnect() is called for each client. Calls made after calling shutdown() may
	 * not be executed.
	 * 
	 * @return	A boolean value indicating success (true) or failure (false).
	 */
	function shutdown():Bool;
}

typedef StatsProps =
{
	//Total number of kilobytes received.
	bw_in:Int,
	//Total number of kilobytes sent.
	bw_out:Int,
	//Total number of bytes sent.
	bytes_in:Int,
	//Total number of bytes received.
	bytes_out:Int,
	//Total number of Real-Time Messaging Protocol (RTMP) messages sent.
	msg_in:Int,
	//Total number of RTMP messages received.
	msg_out:Int,
	//Total number of RTMP messages dropped.
	msg_dropped:Int,
	//Total number of bytes received by the server.
	server_bytes_in:Int,
	//Total number of bytes sent by the server.
	server_bytes_out:Int,
	//Total number of clients connected to an application instance.
	total_connects:Int,
	//Total number of clients who have disconnected from an application instance.
	total_disconnects:Int
	
}